package com.aistar.controller;

import com.aistar.mapper.TravCollectMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.Member;
import com.aistar.pojo.TravCollect;
import com.aistar.pojo.TravelCompletely;
import com.aistar.pojo.Travelnote;
import com.aistar.service.MemberService;
import com.aistar.service.TravelnoteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class TravelnoteController {
    @Autowired
    private TravelnoteService travelnoteService;
    @Autowired
    private MemberService memberService;

    @Autowired
    TravCollectMapper travCollectMapper;

    @Autowired
    TravelnoteMapper travelnoteMapper;

    @GetMapping("travel/{id}")
    public ModelAndView getById(@PathVariable("id") int travelId,HttpServletRequest request){


        System.out.println("获取游记详情中...");
        TravelCompletely travelCompletely = travelnoteService.getbyId(travelId);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);

        ModelAndView mav = new ModelAndView();

        mav.addObject("memberall",member);

        mav.addObject("travelCompletely",travelCompletely);
        mav.setViewName("traveldetails/traveldetails");

        int status = 0;
        TravCollect travCollect = travCollectMapper.selectByPrimaryKey(travelId,userId);
        if(travCollect==null || (travCollect !=null && travCollect.getCollectStatus()==0)){
            status = 0;
        }
        if(travCollect!=null && travCollect.getCollectStatus()==1){
            status = 1;
        }
        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(travelId);
        mav.addObject("status",status);
        mav.addObject("userId",userId);
        mav.addObject("noteId",travelId);
        mav.addObject("num",travelnote.getTravOther2());
        mav.addObject("praise",travelnote.getTravPraise());
        System.out.println("travelnote.getTravOther2():"+travelnote.getTravOther2());
        return mav;
    }

}
